//
//  RecAchiveViewController.m
//  TravelEveryDay
//
//  Created by dlios on 15-10-26.
//  Copyright (c) 2015年 com.lanou3g. All rights reserved.
//

#import "RecAchiveViewController.h"
#import "RecAchiveModel.h"
#import "RecAchiveDetailsViewController.h"
#import "RecViewController.h"
@interface RecAchiveViewController ()

@end

@implementation RecAchiveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self getData];
    self.nameArr = [[NSMutableArray alloc] init];
    self.iconArr = [[NSMutableArray alloc] init];
    
   
}
-(void)backToView
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
#pragma mark - 回到主页点击方法
-(void)backToMainPage
{
    RecViewController *rec = [[RecViewController alloc] init];
    [self.navigationController pushViewController:rec animated:YES];
}
#pragma mark - 数据获取
-(void)getData
{
    [Refresh getdataWithString:[NSString stringWithFormat:@"http://api.breadtrip.com/users/%@/achievement/",self.userId] block:^(id block) {
        RecAchiveModel *model = [RecAchiveModel modelWithDictionary:block];
        NSString *level = [NSString stringWithFormat:@"Lv%@",[[block objectForKey:@"level"] objectForKey:@"value"]];
        NSString *points = [NSString stringWithFormat:@"%@积分",model.points];
        NSString *mil = [NSString stringWithFormat:@"%@km",model.mileage];
        
        //截取到某个字符
        NSArray *arr = [mil componentsSeparatedByString:@"."];
        NSString *myMil = [NSString stringWithFormat:@"%@km",arr[0]];
        
        NSString *country = [NSString stringWithFormat:@"%@国家",model.countries_count];
        NSString *city = [NSString stringWithFormat:@"%@城市",model.cities_count];
        NSString *allLike = [NSString stringWithFormat:@"%@喜欢",model.wish_count];
        NSString *beenLiked = [NSString stringWithFormat:@"%@被喜欢",model.been_liked_count];
        NSString *travelNotes = [NSString stringWithFormat:@"%@游记被喜欢",model.been_recommended_count];
        NSString *download = [NSString stringWithFormat:@"%@下载",model.been_downloaded_count];
        NSString *share = [NSString stringWithFormat:@"%@分享",model.been_shared_count];
        NSString *str = @"";
        //日期转换
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:model.first_trip. integerValue];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"YYYY.MM.dd";
        self.time = [formatter stringFromDate:date];
       //数据数组
        self.leftDataArr = @[level,points, myMil,country, city,allLike, beenLiked,travelNotes, download,share,str];
        
        //国家图标
        for (NSDictionary *dic in [block objectForKey:@"passports"]) {
            NSString *icon = [dic objectForKey:@"icon"];
            [self.iconArr addObject:icon];
        }
        //web页数据
        self.levelUrl = [[[block objectForKey:@"other_info"]objectForKey:@"level"]objectForKey:@"url"];
        self.pointsUrl = [[[block objectForKey:@"other_info"]objectForKey:@"points"]objectForKey:@"url"];
        self.allLikeUrl = [[[block objectForKey:@"other_info"]objectForKey:@"title"]objectForKey:@"url"];
        
        [self setUpPage];
    }];
}
-(void)setUpPage
{
    //背景图片
    UIImageView *image = [[UIImageView alloc] initWithFrame:self.view.frame];
    image.userInteractionEnabled = YES;
    [image sd_setImageWithURL:[NSURL URLWithString:self.coverImageStr] placeholderImage:PLACEHOLDERIMAGE];
    [self.view addSubview:image];
    
    //背景虚化
    UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    visualEffectView.frame = image.frame;
    visualEffectView.alpha = 0.3;
    [image addSubview:visualEffectView];
    
    //背景图片上添加黑色view加深颜色
    UIView *blackView = [[UIView alloc] initWithFrame:image.frame];
    blackView.backgroundColor = [UIColor blackColor];
    blackView.alpha = 0.8;
    [image addSubview:blackView];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 100 * HEIGHT)];
    //头像图标
    UIImageView *iconImage = [[UIImageView alloc] initWithFrame:CGRectMake(155 * WIDTH, 40 * HEIGHT, 60 * WIDTH, 60 * HEIGHT)];
    iconImage.layer.cornerRadius = 30;
    iconImage.layer.masksToBounds = YES;
    iconImage.layer.borderColor = [[UIColor whiteColor]CGColor];
    iconImage.layer.borderWidth = 2;
    [iconImage sd_setImageWithURL:[NSURL URLWithString:self.iconImageStr] placeholderImage:PLACEHOLDERIMAGE];
    [view addSubview:iconImage];
    [blackView addSubview:view];
    
    //底部scrollview
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 164 * WIDTH, SCREEN_WIDTH, SCREEN_HEIGHT)];
    scroll.contentSize = CGSizeMake(0, SCREEN_HEIGHT * 2.8);
    [blackView addSubview:scroll];
    
    //名字
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(100 * WIDTH, 10 * HEIGHT, SCREEN_WIDTH - 200 * WIDTH, 50 * HEIGHT)];
    nameLabel.font = [UIFont boldSystemFontOfSize:24];
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.textColor = [UIColor whiteColor];
    nameLabel.text = self.name;
    [scroll addSubview:nameLabel];
    
    //第一次旅行时间
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(80 * WIDTH, 70 * HEIGHT, SCREEN_WIDTH - 160 * WIDTH, 30 * HEIGHT)];
    timeLabel.font = [UIFont boldSystemFontOfSize:14];
    timeLabel.textAlignment = NSTextAlignmentCenter;
    timeLabel.textColor = [UIColor whiteColor];
    timeLabel.text = [NSString stringWithFormat:@"第一段旅程开始于 %@",self.time];
    [scroll addSubview:timeLabel];
    
    //白线
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(185 * WIDTH, 100 * HEIGHT, 1, SCREEN_HEIGHT * 1.6)];
    lineView.backgroundColor = [UIColor whiteColor];
    [scroll addSubview:lineView];
    
    //图标创建
    for (int i = 0; i < 11; i++) {
        UIView *image = [[UIView alloc] initWithFrame:CGRectMake(162 * WIDTH, (100 * i + 150) * HEIGHT, 50 * WIDTH, 50 * HEIGHT)];
        image.layer.masksToBounds = YES;
        image.layer.cornerRadius = 25;
        image.layer.borderColor = [[UIColor whiteColor]CGColor];
        image.layer.borderWidth = 2;
        image.backgroundColor = [UIColor blackColor];
        [scroll addSubview:image];
        NSArray *arr = @[@"whiteLevel.png",@"jifen.png",@"map.png",@"country.png",@"citys.png",@"myheart.png",@"good.png",@"star.png",@"download.png",@"share.png",@"earth.png"];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10 * WIDTH, 11 * HEIGHT, 30 * WIDTH, 30 * HEIGHT)];
        imageView.image = [UIImage imageNamed:[arr objectAtIndex:i]];
        [image addSubview:imageView];
        
        if (i % 2 == 0) {
            //白线左侧
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50 * WIDTH, (100 * i + 150) * HEIGHT, 130 * WIDTH, 50 * HEIGHT)];
            label.textColor = [UIColor whiteColor];
            label.text = [self.leftDataArr objectAtIndex:i] ;
            label.font = [UIFont boldSystemFontOfSize:22];
            [scroll addSubview:label];
        }else{
            //白线右侧
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(220 * WIDTH, (100 * i + 150) * HEIGHT, 160 * WIDTH, 50 * HEIGHT)];
            label.textColor = [UIColor whiteColor];
            label.font = [UIFont boldSystemFontOfSize:22];
            label.text = [self.leftDataArr objectAtIndex:i] ;
            [scroll addSubview:label];
        }
        
        //等级说明按钮
        if (i == 0) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setTitle:@"等级说明 >" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            button.frame = CGRectMake(60 * WIDTH, 185 * HEIGHT, 100 * WIDTH, 40 * HEIGHT);
            [button addTarget:self action:@selector(levelToWeb) forControlEvents:UIControlEventTouchUpInside];
            [scroll addSubview:button];
        }
        //积分兑换按钮
        if (i == 1) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setTitle:@"积分兑换 >" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            button.frame = CGRectMake(220 * WIDTH, 290 * HEIGHT, 100 * WIDTH, 40 * HEIGHT);
            [button addTarget:self action:@selector(pointsToWeb) forControlEvents:UIControlEventTouchUpInside];
            [scroll addSubview:button];
        }
        //全部喜欢按钮
        if (i == 5) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setTitle:@"全部喜欢 >" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            button.frame = CGRectMake(220 * WIDTH, 690 * HEIGHT, 100 * WIDTH, 40 * HEIGHT);[button addTarget:self action:@selector(allLikeToWeb) forControlEvents:UIControlEventTouchUpInside];
            [scroll addSubview:button];
        }

        
    }
    
    //国家图标
    for (int i = 0; i < self.iconArr.count;i++) {
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake((20 + 50 * i) * WIDTH, 1250 * HEIGHT, 40 * WIDTH, 40 * HEIGHT)];
        [image sd_setImageWithURL:[NSURL URLWithString:[self.iconArr objectAtIndex:i]] placeholderImage:PLACEHOLDERIMAGE];
        image.layer.cornerRadius = 20;
        image.layer.masksToBounds = YES;
        image.layer.borderColor = [[UIColor whiteColor]CGColor];
        image.backgroundColor = [UIColor redColor];
        image.layer.borderWidth = 2;
        [scroll addSubview:image];
    }
    
    //旅行护照个数
    UILabel *countLabel = [[UILabel alloc] initWithFrame:CGRectMake(160 * WIDTH, 1350 * HEIGHT, 80 * WIDTH, 80 * HEIGHT)];
    countLabel.font = [UIFont boldSystemFontOfSize:70];
    countLabel.textColor = [UIColor whiteColor];
    countLabel.text = [NSString stringWithFormat:@"%ld",self.iconArr.count];
    [scroll addSubview:countLabel];
    
    //护照text
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(150 * WIDTH, 1440 * HEIGHT, 150 * WIDTH, 40 * HEIGHT)];
    textLabel.textColor = [UIColor whiteColor];
    textLabel.text = @"简·行护照";
    [scroll addSubview:textLabel];
    
    //最下面英文
    UILabel *lastLabel = [[UILabel alloc] initWithFrame:CGRectMake(65 * WIDTH, 1560 * HEIGHT, 250 * WIDTH, 50 * HEIGHT)];
    lastLabel.text = @"TRAVEL IS A PART OF LIFE RECORD,SHARE AND EXPERIENCE";
    [lastLabel setNumberOfLines:2];
    lastLabel.font = [UIFont systemFontOfSize:14];
    lastLabel.textAlignment = NSTextAlignmentCenter;
    lastLabel.alpha = 0.6;
    lastLabel.textColor = [UIColor whiteColor];
    [scroll addSubview:lastLabel];
    
    
    //回到主页的右侧按钮
    UIButton *bactToMain = [UIButton buttonWithType:UIButtonTypeCustom];
    bactToMain.frame = CGRectMake(330, 10, 30, 30);
    [bactToMain addTarget:self action:@selector(backToMainPage) forControlEvents:UIControlEventTouchUpInside];
    [bactToMain setBackgroundImage:[UIImage imageNamed:@"mainPage.png"] forState:UIControlStateNormal];
    [blackView addSubview:bactToMain];
    
    
    UIButton *backToView = [UIButton buttonWithType:UIButtonTypeCustom];
    backToView.frame = CGRectMake(20, 10, 30, 30);
    [backToView addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    [backToView setBackgroundImage:[UIImage imageNamed:@"left.png"] forState:UIControlStateNormal];
    [blackView addSubview:backToView];
    
}
-(void)levelToWeb
{
    RecAchiveDetailsViewController *detail = [[RecAchiveDetailsViewController alloc] init];
    [self.navigationController pushViewController:detail animated:YES];
    detail.url = self.levelUrl;
}
-(void)pointsToWeb
{
    RecAchiveDetailsViewController *detail = [[RecAchiveDetailsViewController alloc] init];
    [self.navigationController pushViewController:detail animated:YES];
    detail.url = self.pointsUrl;
}
-(void)allLikeToWeb
{
    RecAchiveDetailsViewController *detail = [[RecAchiveDetailsViewController alloc] init];
    [self.navigationController pushViewController:detail animated:YES];
    detail.url = self.allLikeUrl;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
